// 依赖实体类
const SafeString = require('../base/SafeString');
const SafeNumber = require('../base/SafeNumber');
const ArgvItem = require('./ArgvItem');

// 配置类
const config = require('../../config').config;

/**
 * 实体类 ArgvVersion 用于记录参数版本里每一个值的信息
 */
class ArgvVersion {
    constructor(obj) {
        this.version = new SafeString(obj.version, 1, config.argvVersion_length).value;   // 参数版本必须是唯一的非空字符串
        this.type = new SafeString(obj.type, 0, config.argvType_length).value;            // 参数版本必须是唯一的非空字符串
        this.count = new SafeNumber(obj.count).value;                                     // 参数类型的参数个数
        // 以下为解析参数列表的逻辑
        let content = JSON.parse(new SafeString(obj.content, 0, 50000).value);            // 参数版本的参数列表
        this.content = [];
        if (!Array.isArray(content)) {
            throw new TypeError(`Expected Array, got ${typeof content}`);
        }
        if (content.length != this.count) {
            throw new RangeError(`Expected Array length to be at most ${this.count}, got ${content.length}`);
        }
        for (let i = 0; i < content.length; i++) {
            let obj = new ArgvItem(content[i]);
            this.content.push(obj);
        }
    }
}

module.exports = ArgvVersion;